

package com.bei_en.teacher.admin.controller.subject;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.subject.bean.Subject;
import com.bei_en.teacher.admin.model.subject.condition.SubjectCondition;
import com.bei_en.teacher.admin.model.subject.vo.SubjectVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.subject.SubjectService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-9-26 22:19:05
 */
@Controller
@RequestMapping("/api/Subject")
public class SubjectAPPController extends BaseController {

    @Autowired
    SubjectService subjectService;


    @RequestMapping("/subject")
	@ResponseBody
    public JsonResult subject(Model model,@Valid  SubjectCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<SubjectVo> list =new ArrayList<SubjectVo>();
		   list = subjectService.findSubjectPage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   SubjectVo subjectVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   subjectVo=list.get(i);
			   seetingJsonObject(object,"Id",  subjectVo.getId());
	   seetingJsonObject(object,"id",  subjectVo.getId());
	   seetingJsonObject(object,"name",  subjectVo.getName());
	   seetingJsonObject(object,"parent",  subjectVo.getParent());
	   seetingJsonObject(object,"educationId",  subjectVo.getEducationId());
	   seetingJsonObject(object,"createTime",  subjectVo.getCreateTime());
	   seetingJsonObject(object,"provinceId",  subjectVo.getProvinceId());
	   seetingJsonObject(object,"cityId",  subjectVo.getCityId());
	   seetingJsonObject(object,"areaId",  subjectVo.getAreaId());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/subjectAdd")
    @ResponseBody
    public JsonResult subjectAdd(Model model,@Valid  SubjectCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
      		
      	
		    SubjectVo subject=new SubjectVo();
		    
	   subject.setId(vo.getId());
	   subject.setName(vo.getName());
	   subject.setParent(vo.getParent());
	   subject.setEducationId(vo.getEducationId());
//	   subject.setCreateTime(vo.getCreateTime());
	   subject.setProvinceId(vo.getProvinceId());
	   subject.setCityId(vo.getCityId());
	   subject.setAreaId(vo.getAreaId());
		    
			subjectService.insert(subject);
			return new JsonSuccessResult(session.getId(), "成功");
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
    @RequestMapping("/subjectEdit")
    public JsonResult subjectEdit(Model model,@Valid  SubjectCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	SubjectVo subject=new SubjectVo();
	
		subjectService.updateSelective(subject);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
